package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author zhouwei
 * @date 2020-01-03 10:51
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    /**
     * 文件上传
     * @param orderSettings
     */
    @Override
    public void upload(ArrayList<OrderSetting> orderSettings) {
        //遍历集合，一个个导入到数据库
        if(orderSettings!=null&&orderSettings.size()>0) {
            for (OrderSetting orderSetting : orderSettings) {
                long count = orderSettingDao.findCountByordersetting(orderSetting.getOrderDate());
                if (count > 0) {
                    //日期已存在,更新操作
                    orderSettingDao.update(orderSetting);
                }else{
                    //设置已预约人数为0，
                    orderSetting.setReservations(0);
                    orderSettingDao.upload(orderSetting);
                }
            }
        }
    }

    /**
     * 查询预约
     * @param date
     * @return
     */
    @Override
    public List<Map> findDate(String date) {
       List<Map> list=orderSettingDao.findDate(date);
       return list;
    }

    /**
     * 设置预约
     * @param orderSetting
     */
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        //判断是否设置过预约人数
        Date orderDate = orderSetting.getOrderDate();
        //System.out.println(orderDate);
        long num = orderSettingDao.findCountByordersetting(orderDate);
        if(num!=0){
            //如果以进行预约人数设置，则更新
            orderSettingDao.editNumberByDate1(orderSetting);
        }else{
            //没设置过，则新增
            orderSettingDao.editNumberByDate2(orderSetting);
        }
    }


}
